<?php

/**
 * reaction actions.
 *
 * @package    LYPO
 * @subpackage reaction
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 9301 2008-05-27 01:08:46Z dwhittle $
 */
class reactionActions extends sfActions
{

	public function preExecute(){
		$this->setTemplate('result');
	}
	/**
	 * Executes index action
	 *
	 * @param sfRequest $request A request object
	 */
	public function executeSave(sfWebRequest $request)
	{
		$userid = $request->getParameter('userid');
		$statement_id = $request->getParameter('statement_id');
		$content = $request->getParameter('content');
		$embed_link = $request->getParameter('embed_link');
		$embed_code = $request->getParameter('embed_code');
		
		/* @var $statement LypoStatement */
		$result = new LypoResult();
		if($userid && $statement_id && ($content || $embed_code || $embed_link)){
			try {
				/* @var $user User */
				$user = UserPeer::getByContainerString($userid);
				if($user){
					$ret = ReactionPeer::saveReaction($user, $statement_id, $content, $embed_code, $embed_link);
					LypoStatementPeer::updateReactionCount($statement_id);
					$result->setMessage('OK');
				}else{
					$result->setErrorMessage("User not found");
				}
			}catch (Exception $e){
				$result->setMessage($e->getMessage());
			}

		}else{
			$result->setErrorMessage("Not all parameters present in request (userid, statement_id, (content | embed_link | embed_code))");
		}
		$this->result = $result;
		//return $this->renderText($result->toJSON());
	}

	public function executeRespect($request)
	{
		$userid = $request->getParameter('userid');
		$reaction_id = $request->getParameter('reaction_id');
		$result = new LypoResult();
		if($userid && $reaction_id){
			try {
				/* @var $user User */
				$user = UserPeer::getByContainerString($userid);
				$reaction = ReactionPeer::retrieveByPK($reaction_id);
				if($user && $reaction){
					$ret = ReactionPeer::giveRespectToReaction($user, $reaction_id);
					$result->setMessage('OK');
				}else{
					$result->setErrorMessage("User or reaction not found");
				}
			}catch (Exception $e){
				$result->setMessage($e->getMessage());
			}

		}else{
			$result->setErrorMessage("Not all parameters present in request (userid, reaction_id)");
		}
		$this->result = $result;
		//return $this->renderText($result->toJSON());
	}

	public function executeRemoverespect($request)
	{
		$userid = $request->getParameter('userid');
		$reaction_id = $request->getParameter('reaction_id');
		$result = new LypoResult();
		if($userid && $reaction_id){
			try {
				/* @var $user User */
				$user = UserPeer::getByContainerString($userid);
				$reaction = ReactionPeer::retrieveByPK($reaction_id);
				if($user && $reaction){
					$ret = ReactionPeer::giveRespectToReaction($user, $reaction_id);
					$result->setMessage('OK');
				}else{
					$result->setErrorMessage("User or reaction not found");
				}
			}catch (Exception $e){
				$result->setMessage($e->getMessage());
			}

		}else{
			$result->setErrorMessage("Not all parameters present in request (userid, reaction_id)");
		}
		$this->result = $result;
		//return $this->renderText($result->toJSON());
	}



}
